home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v9n05.arc / ATOF.ASM < prev    next >
Assembly Source File  |  1990-02-13  |  5KB  |  142 lines

  1.         title   ATOF - ASCII to Binary Floating Point
  2.         page    55,132
  3.  
  4. ; ATOF.ASM ---  Convert ASCII String to Binary 
  5. ;               Floating Point Number on 80x87 Stack
  6. ;               (also requires FALOG from FALOG.ASM)
  7. ;
  8. ; Copyright (C) 1989 Ziff Davis Communications
  9. ; PC Magazine * Ray Duncan
  10. ; Call with:    DS:SI = address of string in the form
  11. ;               [sign][digits][.[digits]][E|e[sign][exp]]
  12. ;               leading blanks or tabs are ignored
  13. ;
  14. ; Returns:      ST(0) = binary floating point value
  15. ;               DS:SI = address+1 of terminator 
  16. ;
  17. ; Uses:         Nothing
  18. ;
  19. ; This routine gives no warning in the event of 
  20. ; overflow, and terminates on the first invalid character.
  21. ;
  22. ; Make sure coprocessor has been properly initialized
  23. ; with a previous call to INIT87!
  24.  
  25. blank   equ     20h             ; ASCII blank character
  26. tab     equ     09h             ; ASCII tab character
  27.  
  28. _DATA   segment word public 'DATA'
  29.  
  30. int10   dw      10              ; integer constant 10
  31. digit   dw      0               ; current converted digit
  32. places  dw      0               ; number of decimal places
  33.  
  34. _DATA   ends
  35.  
  36. _TEXT   segment word public 'CODE'
  37.  
  38.         assume  cs:_TEXT,ds:_DATA
  39.  
  40.         extrn   falog:near      ; we need FALOG routine 
  41.  
  42.         public  atof
  43. atof    proc    near            ; ASCII to floating point
  44.  
  45.         push    ax              ; save registers
  46.         push    cx
  47.         push    dx
  48.  
  49.         call    convert         ; convert mantissa
  50.  
  51.         neg     dx              ; save -1 * decimal places
  52.         mov     places,dx
  53.  
  54.         or      al,20h          ; fold char to lower case
  55.         cmp     al,'e'          ; is exponent present?
  56.         je      atof1           ; yes, jump
  57.  
  58.         fldz                    ; assume zero exponent
  59.         jmp     atof2
  60.  
  61. atof1:  call    convert         ; convert exponent
  62.  
  63. atof2:  fiadd   places          ; adjust exponent for 
  64.                                 ; dec. places in mantissa
  65.  
  66.         call    falog           ; raise 10 to power
  67.         fmul                    ; exponent * mantissa
  68.  
  69.         pop     dx              ; restore registers
  70.         pop     cx
  71.         pop     ax
  72.         ret                     ; return ST(0) = result
  73.  
  74. atof    endp
  75.  
  76. ;
  77. ; CONVERT:      Called by ATOF to convert ASCII number
  78. ;               with possible sign and/or decimal point
  79. ; Call with:    DS:SI = address of string
  80. ; Returns:      ST(0) = result
  81. ;               AL    = first unconvertable character
  82. ;               DX    = number of digits after decimal point
  83. ;               DS:SI = address+1 of character in AL
  84. ; Uses:         AH, CX
  85. ;
  86. convert proc    near            ; convert numeric field
  87.  
  88.         fldz                    ; initialize result
  89.         xor     cx,cx           ; initialize sign
  90.         mov     dx,-1           ; initialize decimal count
  91.  
  92. conv1:  lodsb                   ; scan off whitespace
  93.         cmp     al,blank        ; ignore leading blanks
  94.         je      conv1
  95.         cmp     al,tab          ; ignore leading tabs
  96.         je      conv1
  97.  
  98.         cmp     al,'+'          ; if + sign proceed
  99.         je      conv2
  100.         cmp     al,'-'          ; is it - sign?
  101.         jne     conv3           ; no, test if numeric
  102.         dec     cx              ; yes, set flag
  103.  
  104. conv2:  lodsb                   ; get next character
  105.  
  106. conv3:  cmp     al,'0'          ; is character valid?
  107.         jb      conv4           ; jump if not '0' to '9'
  108.         cmp     al,'9'
  109.         ja      conv4           ; jump if not '0' to '9'
  110.  
  111.         and     ax,0fh          ; isolate lower four bits
  112.         mov     digit,ax        ; and save digit value
  113.         fimul   int10           ; previous value * 10
  114.         fiadd   digit           ; accumulate new digit
  115.  
  116.         or      dx,dx           ; past decimal point?
  117.         js      conv2           ; no, convert next digit
  118.         inc     dx              ; yes, count digits
  119.         jmp     conv2           ; convert next digit
  120.  
  121. conv4:  cmp     al,'.'          ; is it decimal point?
  122.         jne     conv5           ; no, proceed
  123.         inc     dx              ; indicate decimal found
  124.         jmp     conv2           ; convert more digits
  125.  
  126. conv5:  jcxz    conv6           ; jump if result pos.
  127.         fchs                    ; make result negative
  128.  
  129. conv6:  or      dx,dx           ; decimal point found?
  130.         jns     conv7           ; yes, jump
  131.         xor     dx,dx           ; no, return zero places
  132.         
  133. conv7:  ret                     ; return ST(0) = result
  134.  
  135. convert endp
  136.  
  137. _TEXT   ends
  138.  
  139.         end
  140.  
  141.